#include "stdafx.h"
#include "Player.h"

CPlayer* CPlayer::m_pInstance = NULL;

CPlayer* CPlayer::GetInstance(void)
{
	if(!m_pInstance)
		m_pInstance = new CPlayer();

	return m_pInstance;
}

void CPlayer::PlayerInit()
{
	this->m_mPlayerMat = new matrix4f;
	this->m_fMass = 1.0f;
	this->m_fJetPackEnergy = 20.0f;
	this->m_fEnergyRecharge = 2.0f;
	this->m_bIsRecharging = false;
	this->m_bJumping = false;
}

void CPlayer::Update(float fDeltaTime)
{
	//Check Health

	//Check Energy on Jetpack
	if(m_bIsRecharging)
	{
		m_fEnergyRecharge -= fDeltaTime;
		if(m_fEnergyRecharge <= 0.0f)
		{
			this->m_fJetPackEnergy += 4.0f;
			m_fEnergyRecharge = 2.0f;
			if(m_fJetPackEnergy > 20.0f)
			{
				m_fJetPackEnergy = 20.0f;
				m_bIsRecharging = false;
			}
		}
	}

	if(m_bJumping)
	{
		Jump(fDeltaTime);
	}

	else
	{
		this->m_mPlayerMat->axis_pos.y -= 9.8f*fDeltaTime;
		if(this->m_mPlayerMat->axis_pos.y <= 1.0f)
		{
			this->m_mPlayerMat->axis_pos.y = 1.0f;
		}
	}
	
}

void CPlayer::Jump(float fDeltaTime)
{
	//Add a force to the player
	this->m_mPlayerMat->axis_pos.y += 3.0f*fDeltaTime;
	this->m_fJetPackEnergy -= 5.0f*fDeltaTime;

	if(m_fJetPackEnergy <= 0.0f)
	{
		m_bJumping = false;
		m_fJetPackEnergy = 0.0f;
		m_bIsRecharging = true;
	}
}

//Accessor
vec3f CPlayer::GetPlayerPosition(void)
{
	return this->m_mPlayerMat->axis_pos;
}

matrix4f* CPlayer::GetPlayerMat(void)
{
	return m_mPlayerMat;
}


//Mutators
void CPlayer::SetMatrix(matrix4f* mat)
{
	this->m_mPlayerMat = mat;
}

void CPlayer::SetOrigin(vec3f Pos)
{
	this->m_mPlayerMat->axis_pos = Pos;
}

void CPlayer::SetForward(vec3f v)
{
	this->m_mPlayerMat->axis_z = v;
}

void CPlayer::TranslatePlayer(vec3f v)
{
	//X axis translation, first create an X axis
	if(v.x != 0)
	{
		this->m_mPlayerMat->axis_pos.x += v.x*m_mPlayerMat->axis_x.x;
		//this->m_mPlayerMat->axis_pos.y += v.x*m_mPlayerMat->axis_x.y;
		this->m_mPlayerMat->axis_pos.z += v.x*m_mPlayerMat->axis_x.z;
	}

	//Add in Z movement
	this->m_mPlayerMat->axis_pos.x += v.z*this->m_mPlayerMat->axis_z.x;
	//this->m_mPlayerMat->axis_pos.y += v.z*this->m_mPlayerMat->axis_z.y;
	this->m_mPlayerMat->axis_pos.z += v.z*this->m_mPlayerMat->axis_z.z;
}

void CPlayer::SetAxes(vec3f Yvec, vec3f Zvec)
{
	this->m_mPlayerMat->axis_y = Yvec;
	this->m_mPlayerMat->axis_z = Zvec;
	this->m_mPlayerMat->axis_x = cross_product(Yvec, Zvec);
}
